from physics import * 
add_library('sound')
position = [[50,250],[100,200],[200,300],[200,400],[100,500]];
lastx = 0
for i in xrange(0,500):
    position.append([int(random(0,150)),-i*100])
    position.append([int(random(150,300)),-i*100])
    
    #position.append([200,-i*100])
for i in xrange(0,2):
    position.append([int(random(0,300)), i*100])
    position.append([int(random(0,300)),-i*100])
vx = 50
w ,h = 400, 600
flag = {'start':True}
def preload():
    
    plat = loadImage('platform.png');
    player = loadImage('doodler-guy.png');

def setup():
    global bgm
    bgm = SoundFile(this, 'bgm.mp3')
    bgm.play()
   
    size(400, 600)
    global img
    global player
    global plat
    global play_position
    global speed
    global acc 
    global state
    global tran_y 
    global score
    global is_end
    score = 0
    is_end = False
    tran_y = 0
    state  = 'jump'
    acc = 0
    speed = 0
    play_position = PVector(100, 200)
    # bgm =  SoundFile(this,'qq.mp3')
    # bgm.loop()
    img = loadImage('background.png');
    player = loadImage('poji.png');# poji
    plat = loadImage('platform.png');
    global tran_sum 
    tran_sum = 0
    global last_y 
    last_y = -1
    
def draw():
    global speed
    global acc 
    global tran_y 
    global score 
    global is_end
    global tran_sum
    global last_y 
    last  = play_position.y 
    play_position.y +=speed;
    if play_position.y > h :
        is_end = True
    if score < 900*100  and  is_end:
        image(img, 0, 0)
        #stroke(0)
        fill(0)
        textSize(30)
        title ='Game Over'
        text(title,w/2 - textWidth(title)/2,h/2)
        
    elif score > 450*100 and is_end:
        image(img, 0, 0)
        #stroke(0)
        fill(0)
        textSize(30)
        title ='Win'
        text(title,w/2 - textWidth(title)/2,h/2)
    else:
 
        
                
        speed += acc;
        
        image(img, 0, 0)
        if flag['start']:
            textSize(20)
            title ='Press y to start, a d to move'
            text(title,w/2 - textWidth(title)/2,h/2)
        image(player, play_position.x, play_position.y)
        
        for x,y in position :
            if abs(y- play_position.y)>5000:
                
                continue
            
            image(plat, x, y)
            if speed > 0: 
             
                p = (play_position.x+0.5*player.width,play_position.y+player.height)
                rectangle = top_left_rect((x,y),plat.width,plat.height)
  
                if play_position.y+player.height > y:
                    if last < y:
                        
                        if speed>0 and  x<  play_position.x+0.5*player.width < x + plat.width:
                            global state
                            state='stop'
                            # speed = 0
                            # acc= 0 
                            speed = - 20
                            acc = 2
                            print('before last y ',last_y)
                            print('before last y,y ',y)
                            i = position.index([x,y])
                            if last_y!=i:
                                score+=100
                            last_y = i
                            print('last y ',last_y)
                            #score = max(score,-y)
                            if y < 300:
                                tran_y = 300 - y
                            break
                            
                # if  point_collide_with_rect(p,rectangle):
                #     print((x,y))
                #     print(p)
                #     print('collide')
                #     global state
                #     state='stop'
                #     # speed = 0
                #     # acc= 0 
                #     speed = - 20
                #     acc = 2
    if tran_y > 0:     
         
        for i in range(len(position)):
            position[i][1]+= tran_y
        play_position.y += tran_y
        tran_sum += tran_y
    tran_y = 0    
    fill(0)
    textSize(30)
    text( str(score), 0.65*w , 100)
            
def keyPressed():
    global acc
    global speed 

    global play_position
    if key == 'y':
        if flag['start']:
            speed = - 20
            acc = 0.25
            flag['start'] = False
            
        
    if key == 'a':
        play_position.x -= vx
    if key == 'q':
        play_position.x -= vx*2
    if key == 'd':
        play_position.x += vx
    if key == 'e':
        play_position.x += vx*2
    play_position.x += w
    play_position.x %= w
    global is_end
    if key == 's':
        if is_end:
            is_end = False
            print('end')
            print(is_end)
            play_position.y = 200
    
def mousePressed():
    global acc
    global speed 
    speed = - 20
    acc = 0.25
